package com.example.guessnumberplus;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.xml.transform.Result;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button Btn_start;
    Button Btn_end;
    Button Btn_qr;
    Button Btn_da;
    EditTextEx EditText1,EditText2,EditText3,EditText4;
    TextView Tv_result;
    Set numberSet = new HashSet();
    int [] intput = new int[4];
//    int max,min,num;
    int [] num = new int[4];
    int numNunmer = 0;
    int numPosition = 0;
    boolean[] used = new boolean[4];
    String result_str = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Btn_start = findViewById(R.id.btn_start);
        Btn_end = findViewById(R.id.btn_end);
        Btn_qr = findViewById(R.id.btn_qr);
        Btn_da = findViewById(R.id.btn_da);

        EditText1 = findViewById(R.id.editText1);
        EditText2 = findViewById(R.id.editText2);
        EditText3 = findViewById(R.id.editText3);
        EditText4 = findViewById(R.id.editText4);

        EditText1.setNextEditText(EditText2);
        EditText2.setPrevEditText(EditText1);
        EditText2.setNextEditText(EditText3);
        EditText3.setPrevEditText(EditText2);
        EditText3.setNextEditText(EditText4);
        EditText4.setPrevEditText(EditText3);

        Tv_result = findViewById(R.id.Tv_result);

        Btn_start.setOnClickListener(this);
        Btn_end.setOnClickListener(this);
        Btn_qr.setOnClickListener(this);
        Btn_da.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:
//                max = 100;
//                min = 0;
                Btn_qr.setEnabled(true);
                Btn_end.setEnabled(true);
                Btn_start.setEnabled(false);
                Btn_da.setEnabled(true);

                Random random = new Random();
//                num = random.nextInt(100)+1;
                for(int i=0;i<num.length;i++){
                    num[i]=random.nextInt(10);
                }
                result_str = "";
                break;

            case R.id.btn_qr:
                //设置检查方法
                String s = EditText1.getText().toString();
                int value = Integer.parseInt(s);
                intput[0] = value;
                s = EditText2.getText().toString();
                value = Integer.parseInt(s);
                intput[1] = value;
                s = EditText3.getText().toString();
                value = Integer.parseInt(s);
                intput[2] = value;
                s = EditText4.getText().toString();
                value = Integer.parseInt(s);
                intput[3] = value;
                if(intput.length<4){
                    Toast.makeText(MainActivity.this,"请输入完整的数字",Toast.LENGTH_LONG).show();
                }else {
                    numNunmer = 0;
                    numPosition = 0;
                    panduan(intput);
                }

                break;
            case R.id.btn_end:
                //设置是否确认退出的窗口
                AlertDialog dialog ;
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setTitle("退出游戏")
                        .setMessage("是否确认退出游戏")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                MainActivity.this.finish();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                dialog = builder.create();
                dialog.show();
                break;

            case R.id.btn_da:{
                    result_str = result_str + "\n" + "正确的数字答案为：" + num[0] + num[1] + num[2] + num[3];
                    Tv_result.setText(result_str);
                break;
            }

        }
    }

    public void panduan(int[] intput) {
//        String tempStr = "请输入"+min+"~"+max+"之间的整数";
//        int tempNum = Integer.parseInt(intput);
//        if(tempNum<0||tempNum>100){
//            Toast.makeText(MainActivity.this,tempStr,Toast.LENGTH_LONG).show();
//        }else if(tempNum<num){
//            min = tempNum;
//            result_str = result_str + "\n" + "猜错了，比正确的数字小";
//            Tv_result.setText(result_str);
//
//        }else if(tempNum==num){
//            result_str = result_str + "\n" + "猜对了，是"+num;
//            Tv_result.setText(result_str);
//        }else if(tempNum>num){
//            max = tempNum;
//            result_str = result_str + "\n" + "猜错了，比正确的数字大";
//            Tv_result.setText(result_str);
//
//        }
        //判断数字是否正确
        for (int k=0;k<4;k++){
            used[k]=false;
        }

        for (int s: num){
            numberSet.add(s);
        }

        for (int n:intput){
            if(numberSet.contains(n)){
                numNunmer++;
            }
        }


        //判断位置是否正确
        for (int i=0;i<4;i++){
            if(!used[i]){
                for (int j=0;j<4;j++){
                    if(!used[j] && intput[i]==num[j]){
                        numPosition++;
                        used[j]=true;
                        break;
                    } } } }

        if(numNunmer==4&&numPosition==4){
            result_str = result_str + "\n" + "恭喜你才对了，正确数字是："+num[0]+num[1]+num[2]+num[3];
            Tv_result.setText(result_str);
        }else {
            result_str = result_str + "\n" + "有" + numNunmer + "个数字存在" + numPosition + "个数字位置正确";
            Tv_result.setText(result_str);
        }
        numberSet.clear();
    }


}